package com.webmall.servlet.pages;

import com.webmall.entity.CateEntity;
import com.webmall.entity.ProdEntity;
import com.webmall.service.webmall_CateDao;
import com.webmall.service.webmall_ProdDao;

import javax.persistence.criteria.CriteriaBuilder;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet("/indexSelectCate")
public class indexSelectCate extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        ArrayList<CateEntity> fatherList = webmall_CateDao.showCate("father");
        request.setAttribute("fatherList",fatherList);

        ArrayList<CateEntity> childList = webmall_CateDao.showCate("child");
        request.setAttribute("childList",childList);
        String fid = request.getParameter("fid");//点击首页父分类
        String cid = request.getParameter("cid");//点击首页子分类


        request.setAttribute("fid",fid);
        request.setAttribute("cid",cid);
       // String selectID = fid != null?fid:cid;
        //需要根据fid和cid遍历产品表
        String id = "";
        int cateFlag=0;
        ArrayList<ProdEntity> pList = null;
        if (null != fid){
            id = fid;
            cateFlag=0;
        }else {
            id =  cid;
            cateFlag=1;

        }

        pList = webmall_ProdDao.selectByCATEId(cateFlag,id);

        request.setAttribute("title",webmall_CateDao.selectById(id).getCATE_NAME());//在首页上根据选择分类不同显示title标签

        if (null == pList){
            System.out.println("plist null");
        }else {
           // System.out.println("plist  not null");
            request.setAttribute("plist",pList);


        }


        request.getRequestDispatcher("proList.jsp").forward(request,response);
    }
}
